// ignore_for_file: unrelated_type_equality_checks

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:yt_devops/model/info_item.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/site_elec_price_setting_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/enums.dart';

class SiteListItemView extends StatelessWidget {
  const SiteListItemView(
      {required this.model,
      required this.onItemTaped,
      required this.type,
      super.key});
  final SiteElecPriceSettingModel model;
  final void Function(SiteElecPriceSettingModel model) onItemTaped;
  final ElecPriceType? type;

  List<InfoItem> get infoLists {
    var effectiveTime = '';
    if (model.effectiveTimeList != null &&
        model.effectiveTimeList!.isNotEmpty) {
      for (var i = 0; i < model.effectiveTimeList!.length; i++) {
        var effectiveTimeValue =
            model.effectiveTimeList![i].effectiveTime ?? 'defaultValue';
        var expirationTimeValue =
            model.effectiveTimeList![i].expirationTime ?? '';
        effectiveTime += '$effectiveTimeValue $expirationTimeValue';

        // 在最后一个条目之后不要加逗号
        if (i < model.effectiveTimeList!.length - 1) {
          effectiveTime += ', ';
        }
      }
    }
    return [
      InfoItem(ResKey.siteSettingEffectiveDate.tr, effectiveTime),
      InfoItem(ResKey.siteSettingUpdateUser.tr, model.operator ?? ''),
      InfoItem(ResKey.siteSettingUpdateTime.tr, model.lastOperationTime ?? ''),
    ];
  }

  String? getStatus() {
    if (type == ElecPriceType.mains) {
      return $enumDecodeNullable($MainsElecStatusTypeEnumMap, model.status)
          ?.label;
    } else if (type == ElecPriceType.internet) {
      return $enumDecodeNullable($InternetElecStatusTypeEnumMap, model.status)
          ?.label;
    } else {
      return $enumDecodeNullable($ChargeIdElecStatusTypeEnumMap, model.status)
          ?.label;
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        padding: EdgeInsets.fromLTRB(16.w, 0, 16.w, 16.w),
        margin: const EdgeInsets.only(left: 16,right: 16,bottom: 10),
        decoration: const BoxDecoration(
            color: Colour.white,
            borderRadius: BorderRadius.all(Radius.circular(10))),
        child: Column(
          children: [
            SizedBox(
              height: 50.h,
              child: Row(
                children: [
                  Expanded(
                      child: Text(
                    model.name ?? '',
                    style: TextStyle(
                        color: Colour.mainTextColor,
                        fontSize: 18.sp,
                        fontWeight: FontWeight.w500),
                    overflow: TextOverflow.ellipsis,
                  )),
                  Padding(
                    padding: EdgeInsets.only(left: 10.w),
                    child: statusWidget(),
                  )
                ],
              ),
            ),
            ...infoLists.map(
              (e) => Padding(
                padding: EdgeInsets.only(bottom: 4.h),
                child: Row(
                  children: [
                    SizedBox(
                      width: 100.w,
                      child: Text(e.label, style: TextStyles.lf14),
                    ),
                    Expanded(
                      child: Text(
                        e.value,
                        style: TextStyles.f14,
                        softWrap: true,
                      ),
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
      onTap: () => onItemTaped(model),
    );
  }

  Widget statusWidget() {
    String? label = getStatus();

    Color color = Colour.mainTextColor;
    if(label ==  ResKey.siteSettingEffective.tr){
      color = const Color(0xFF00AE66);
    }else if(label ==  ResKey.siteSettingUnEffective.tr){
      color = const Color(0xFFFA3F3F);
    }
    // switch (label) {
    //   case ResKey.siteSettingEffective.tr:
    //     color = const Color(0xFF00AE66);
    //     break;
    //   case ResKey.siteSettingUnEffective.tr:
    //     color = const Color(0xFFFA3F3F);
    //     break;
    //   default:
    //     color = Colour.labelTextColor;
    //     break;
    // }
    return Container(
      alignment: Alignment.center,
      height: 16.h,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(2),
        color: color?.withOpacity(0.1),
      ),
      padding: EdgeInsets.symmetric(horizontal: 4.h),

      child: Text(
        label ?? ResKey.unknownState.tr,
        style: TextStyles.styleWith(12.sp, color: color!),
      ),
    );
  }
}
